package com.scitc.pleasurevideo.dialog;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scitc.pleasurevideo.R;
import com.scitc.pleasurevideo.adapter.DialogSeetingRadioAdapter;
import com.scitc.pleasurevideo.data.ETMan;
import com.scitc.pleasurevideo.util.ConstUtil;

import java.util.Arrays;
import java.util.List;

public class DialogSettingRadioAndRemaber {

    private Context context;

    private NewsPopupWindow popupWindow;

    private TextView sureBtn;
    private TextView tvExitBtn;
    private TextView tvTitle;
    private ListView lvSelectList;
    private DialogSeetingRadioAdapter mAdapter;
    private List<String> items;
    private Integer defaultSelect;
    private String titleStr;
    private RelativeLayout rlRemaber;
    private CheckBox cbCheckbox;

    private OnSettingComplete onSettingComplete;

    public interface OnSettingComplete{
        void onSettingComplete(String selectandisremember);
    }
    public DialogSettingRadioAndRemaber(View view, Context context, String titleStr, List<String> items, OnSettingComplete onSettingComplete) {
        super();
        this.context = context;
        this.onSettingComplete=onSettingComplete;
        this.items=items;
        this.titleStr=titleStr;
        this.defaultSelect=0;
        initView();
        showShare(view, 0, 0, 0);
    }
    public DialogSettingRadioAndRemaber(View view, Context context, String titleStr, String[] array, OnSettingComplete onSettingComplete) {
        super();
        this.context = context;
        this.onSettingComplete=onSettingComplete;
        this.items= Arrays.asList(array);
        this.titleStr=titleStr;
        this.defaultSelect=0;
        initView();
        showShare(view, 0, 0, 0);
    }

    public DialogSettingRadioAndRemaber(View view, Context context, String titleStr, List<String> items, Integer defaultSelect, OnSettingComplete onSettingComplete) {
        super();
        this.context = context;
        this.onSettingComplete=onSettingComplete;
        this.items=items;
        this.defaultSelect=defaultSelect;
        this.titleStr=titleStr;
        initView();
        showShare(view, 0, 0, 0);
    }
    public DialogSettingRadioAndRemaber(View view, Context context, String titleStr, String[] array, Integer defaultSelect, OnSettingComplete onSettingComplete) {
        super();
        this.context = context;
        this.onSettingComplete=onSettingComplete;
        this.items= Arrays.asList(array);
        this.defaultSelect=defaultSelect;
        this.titleStr=titleStr;
        initView();
        showShare(view, 0, 0, 0);
    }

    /**
     * 显示位置
     * @param parent
     * @param x
     * @param y
     * @param postion
     */
    public void showShare(View parent, int x, int y, int postion) {
        // 设置popwindow显示位置
        popupWindow.showAtLocation(parent, 0, x, y);
        // 获取popwindow焦点
        popupWindow.setFocusable(true);
        // 设置popwindow如果点击外面区域，便关闭。
        popupWindow.setOutsideTouchable(false);
        popupWindow.update();
    }

    /**
     * 初始化
     */
    private void initView() {
        //初始化界面
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View popView = layoutInflater.inflate(R.layout.dialog_setting_radio_checkbox_and_remaber, null);
        popupWindow = new NewsPopupWindow(popView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true);
        popupWindow.setBackgroundDrawable(new ColorDrawable(0));
        popupWindow.setAnimationStyle(R.style.default_dialog_show);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(false);
        //初始化控件
        initView(popView);
        //点击事件
        onclick(popView);

    }

    /**
     * 点击事件
     * @param popView
     */
    private void onclick(View popView) {
        //退出按钮的点击事件
        tvExitBtn.setOnClickListener(v -> popupWindow.dismiss());
        //listview--每一个item-的点击事件
        lvSelectList.setOnItemClickListener((adapterView, view, i, l) -> mAdapter.setPostionIndex(i));
        //确定按钮的点击事件
        sureBtn.setOnClickListener(view -> {
            String isRemember;
            if(cbCheckbox.isChecked()){
                isRemember="true";
            }else {
                isRemember="false";
            }
            onSettingComplete.onSettingComplete(mAdapter.getPostionIndex()+","+isRemember);
            popupWindow.dismiss();
        });
        //设置checkbox的点击事件
        rlRemaber.setOnClickListener(view -> cbCheckbox.setChecked(!cbCheckbox.isChecked()));
    }

    /**
     * 初始化控件
     * @param popView
     */
    private void initView(View popView) {
        sureBtn = popView.findViewById(R.id.tvSureBtn);
        tvExitBtn = popView.findViewById(R.id.tvExitBtn);
        tvTitle = popView.findViewById(R.id.title);
        lvSelectList = popView.findViewById(R.id.lvSelectList);
        rlRemaber = popView.findViewById(R.id.rlRemaber);
        cbCheckbox = popView.findViewById(R.id.cbCheckbox);
        mAdapter=new DialogSeetingRadioAdapter(context,items);
        lvSelectList.setAdapter(mAdapter);
        mAdapter.setPostionIndex(defaultSelect);
        tvTitle.setText(titleStr);
    }

}
